//
//  BombBtn.m
//  QQ消息角标移除效果
//
//  Created by 许树铎 on 16/3/3.
//  Copyright © 2016年 许树铎. All rights reserved.
//

#import "BombBtn.h"

@interface BombBtn ()

/** 小圆 */
@property (nonatomic, weak) UIView *smallCircle;
/** 路径 */
@property (nonatomic, weak) CAShapeLayer *shapL;


@end

@implementation BombBtn

- (CAShapeLayer *)shapL {
    if (!_shapL) {
        CAShapeLayer *shapL = [CAShapeLayer layer];
        shapL.fillColor = [UIColor redColor].CGColor;
        [self.superview.layer insertSublayer:shapL atIndex:0];
        _shapL = shapL;
    }
    return _shapL;
}

- (void)awakeFromNib {
    // 设置圆角
    self.layer.cornerRadius = self.bounds.size.width * 0.5;
    // 设置背景颜色
    [self setBackgroundColor:[UIColor redColor]];
    // 设置文字颜色
    [self setTintColor:[UIColor whiteColor]];
    // 关闭自动布局
    
    
    // 添加拖动手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self addGestureRecognizer:pan];
    
    // 添加小圆
    UIView *smallCircle = [[UIView alloc] init];
    // 设置与角标一样颜色
    smallCircle.backgroundColor = self.backgroundColor;
    // 设置一样大小
    smallCircle.frame = self.frame;
    // 设置与角标一样圆角值
    smallCircle.layer.cornerRadius = self.layer.cornerRadius;
    // 让smallCircle在底部
    [self.superview insertSubview:smallCircle belowSubview:self];
    self.smallCircle = smallCircle;
}

- (void)pan:(UIPanGestureRecognizer *)pan {
    // 实现平移
    // 获取拖动的坐标
    CGPoint transP = [pan translationInView:self];
    CGPoint center = self.center;
    center.x += transP.x;
    center.y += transP.y;
    self.center = center;
    
    // 拖动坐标复位
    [pan setTranslation:CGPointZero inView:self];
    
    // 取得两圆间距
    CGFloat distance = [self distanceWithSmallCircle:self.smallCircle bigCircle:self];
    
    // 计算出小圆的半径（越来越小）
    CGFloat smallR = self.bounds.size.width * 0.5;
    smallR = smallR - distance / 10.0;
    self.smallCircle.bounds = CGRectMake(0, 0, smallR * 2, smallR * 2);
    self.smallCircle.layer.cornerRadius = smallR;
    
    if (self.smallCircle.hidden == NO) {
        // 画两圆的路径
        UIBezierPath *path = [self pathWithSmallCircle:self.smallCircle bigCircle:self];
        self.shapL.path = path.CGPath;
    }
    
    if (distance > 60) {
        self.smallCircle.hidden = YES;
//        self.shapL.hidden = YES;
        [self.shapL removeFromSuperlayer];
    } else {
        self.smallCircle.hidden = NO;
        self.shapL.hidden = NO;
    }
    
    // 当手指松开
    if (pan.state == UIGestureRecognizerStateEnded) {
        // 先把线移除
        [self.shapL removeFromSuperlayer];
        if (distance <= 60) {
            // 复位
            self.smallCircle.hidden = YES;
            [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
                self.center = self.smallCircle.center;
            } completion:^(BOOL finished) {
                // 显示小圆
                self.smallCircle.hidden = NO;
            }];
        } else {
            // 让大圆先变透明
            self.backgroundColor = [UIColor clearColor];
            self.titleLabel.alpha = 0;
            // 销毁，播放动画
            UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.bounds];
            NSLog(@"self.bounds: %@", NSStringFromCGRect(self.bounds));
            NSLog(@"smallC.center.point: %@", NSStringFromCGPoint(self.smallCircle.center));
            NSMutableArray *imageArray = [NSMutableArray array];
            for (int i = 0; i < 5; i++) {
                UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"unreadBomb_%d", i + 1]];
                [imageArray addObject:image];
            }
            imageV.animationImages = imageArray;
            imageV.animationDuration = 0.5;
            [imageV startAnimating];
            [self addSubview:imageV];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.45 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self removeFromSuperview];
            });
        }
    }
}

// 计算两个圆之间的距离
- (CGFloat)distanceWithSmallCircle:(UIView *)smallC bigCircle:(UIView *)bigC {
    // x轴方向偏移量
    CGFloat offsetX = bigC.center.x - smallC.center.x;
    // y轴方向偏移量
    CGFloat offsetY = bigC.center.y - smallC.center.y;
    // 勾股定理求出距离
    return sqrt(offsetX * offsetX + offsetY * offsetY);
}
         
//给定两个圆,描述一个不规则的路径
- (UIBezierPath *)pathWithSmallCircle:(UIView *)smallC bigCircle:(UIView *)bigC {
    
    CGFloat x1 = smallC.center.x;
    CGFloat y1 = smallC.center.y;
    
    CGFloat x2 = bigC.center.x;
    CGFloat y2 = bigC.center.y;
    
    CGFloat d = [self distanceWithSmallCircle:smallC bigCircle:bigC];
    
    if (d <= 0) {
        return nil;
    }
    
    CGFloat cosθ = (y2 - y1) / d;
    CGFloat sinθ = (x2 - x1) / d;
    
    CGFloat r1 = smallC.bounds.size.width * 0.5;
    CGFloat r2 = bigC.bounds.size.width * 0.5;
    
    //创建点
    CGPoint pointA = CGPointMake(x1 - r1 * cosθ, y1 + r1 * sinθ);
    CGPoint pointB = CGPointMake(x1 + r1 * cosθ, y1 - r1 * sinθ);
    CGPoint pointC = CGPointMake(x2 + r2 * cosθ, y2 - r2 * sinθ);
    CGPoint pointD = CGPointMake(x2 - r2 * cosθ, y2 + r2 * sinθ);
    CGPoint pointO = CGPointMake(pointA.x + d * 0.5 * sinθ, pointA.y + d * 0.5 * cosθ);
    CGPoint pointP = CGPointMake(pointB.x + d * 0.5 * sinθ, pointB.y + d * 0.5 * cosθ);
    
    //描述路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    //AB
    [path moveToPoint:pointA];
    [path addLineToPoint:pointB];
    //BC(曲线)
    [path addQuadCurveToPoint:pointC controlPoint:pointP];
    //CD
    [path addLineToPoint:pointD];
    //DA(曲线)]
    [path addQuadCurveToPoint:pointA controlPoint:pointO];
    
    return path;
}


// 重写高亮方法：不高亮
- (void)setHighlighted:(BOOL)highlighted {
    
}

@end



































